#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

ll qmi(ll a, ll b)
{
    ll res = 1;
    while(b)
    {
        res = res * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return res;
}

void solve()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int i = 0;
    string a = "map", b = "pie", c = "mapie";
    int ans = 0;
    while(i < n)
    {
        if(i + 5 <= n && s.substr(i, 5) == c)
        {
            i += 5;
            ans++;
        }
        else if(i + 3 <= n && (s.substr(i, 3) == a || s.substr(i, 3) == b)){
            i += 3;
            ans++;
        } 
        else i++;
    }
    cout << ans << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--)solve();
    return 0;
}